package com.swallow.auth.infrastructure.mysql.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author: 18310740596@163.com
 * @date: 2024-05-11 21:54:41
 * @version: v1.0
 * @describe: 自动填充属性处理器(mybatis-plus)
 */
@Component
public class AutoFillFieldHandler implements MetaObjectHandler {
    private static final String CREATED_TIME_FIELD = "createTime";
    private static final String CREATED_USER_FIELD = "createUser";
    private static final String UPDATED_TIME_FIELD = "updateTime";
    private static final String UPDATED_USER_FIELD = "updateUser";

    private static final String DELETE_FLAG_FIELD = "deleteFlag";

    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取当前登陆的用户信息
        String userNo= "admin001"/*AccountContext.getCurrentUser()*/;
        this.strictInsertFill(metaObject, CREATED_USER_FIELD, String.class, userNo);
        this.strictInsertFill(metaObject, UPDATED_USER_FIELD, String.class, userNo);
        this.strictInsertFill(metaObject, CREATED_TIME_FIELD, LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, UPDATED_TIME_FIELD, LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, DELETE_FLAG_FIELD, Long.class, 0L);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 获取当前登陆的用户信息
        String userNo= "admin001"/*AccountContext.getCurrentUser()*/;
        this.strictUpdateFill(metaObject, UPDATED_TIME_FIELD, LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, UPDATED_USER_FIELD, String.class, userNo);
    }
}
